//
//  ViewController.m
//  LBNavigationBarGradientChangeDemo
//
//  Created by liubo on 2022/11/21.
//

#import "ViewController.h"

@interface ViewController () <UITableViewDelegate, UITableViewDataSource>
{
    CGFloat imageHeight;
}

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UIView *navigationBar;

@property (nonatomic, strong) UIImageView *headerImgView;

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    imageHeight = CGRectGetWidth(self.view.bounds) * 1707/1280;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.navigationBar];
    // Do any additional setup after loading the view.
}

#pragma mark - UITableViewDelegate, UITableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat alpha = 0;
    /*这里的120 没有特殊的意义，100 是 从导航栏完全透明到 导航栏完全不透明的过程中
     tableView 滚动过的偏移量， 20 是 图片底部还出20 的时候，完全展示导航栏
     120 = 100 + 20
     ，如果是图片恰好隐藏的时候，导航栏完全不透明，则120 改为100即可
     100 也是可以灵活设置的
     */
    if (scrollView.contentOffset.y + 84 > imageHeight - 120) {
        alpha = (scrollView.contentOffset.y  + 88   -  (imageHeight - 120))/100;
        alpha = MIN(1, alpha);
    }
    self.navigationBar.alpha = alpha;
}

#pragma mark - lazy load

- (UIImageView *)headerImgView
{
    if (!_headerImgView) {
        //1280 * 1707 图片的宽高
        _headerImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), imageHeight)];
        _headerImgView.image = [UIImage imageNamed:@"B195C914-ACBD-481C-BF3B-424497C3000E1668675100"];
    }
    return _headerImgView;
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        _tableView.tableHeaderView = self.headerImgView;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (UIView *)navigationBar
{
    if (!_navigationBar) {
        _navigationBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 84)];
        _navigationBar.backgroundColor = [UIColor cyanColor];
        _navigationBar.alpha = 0;
    }
    return _navigationBar;
}


@end
